/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2006
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 * Filename:
 * ---------
 * drm_interface.c
 *
 * Project:
 * --------
 *   MAUI
 *
 * Description:
 * ------------
 *   This file is intends for DRM service, wrapper layer.
 *
 * Author:
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *******************************************************************************/
#include "kal_release.h"        /* Basic data type */

#include "fs_type.h"
#include "fat_fs.h"
/* kal_release/fs_type/fat_fs.h MUST be out of __DRM_SUPPORT for non-DRM projects */
#ifdef __DRM_SUPPORT__
 

#include "App_buff_alloc.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h" /* Task message communiction */
#include <stdarg.h>

#include "nvram_user_defs.h"
#include "app_datetime.h"
#include "app_mine.h"
#include "app_str.h"

#include "drm_def.h"
#include "drm_gprot.h"
#include "drm_prot.h"
#include "drm_util.h"
#include "Drm_main.h"
#include "drm_msg.h"

/*
 * global variable
 */

/*
 * local variable
 */


/*****************************************************************************
 * FUNCTION
 *  DRM_process_object
 * DESCRIPTION
 *  generic DRM message/content handler
 * PARAMETERS
 *  type                [IN]    Type of process
 *  content_type        [IN]    HTTP content type, with null terminator
 *  ...                 [IN]    
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_process_object(drm_process_type type, kal_uint8 *content_type, ...)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    va_list list;
    drm_proc_enum result;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    DRM_TRACE("process %s\n", (char*)content_type, 0, 0);

    va_start(list, content_type);
    result = drm_core_process_object(type, content_type, list);
    va_end(list);
    
    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_install_object
 * DESCRIPTION
 *  generic DRM message/content handler
 * PARAMETERS
 *  is_async                [IN]        install asynchronously or not; if true, callback must be given
 *  content_type            [IN]        HTTP content type, with null terminator
 *  serial                  [OUT]       a serial number of this install job
 *  src_path                [IN]        source file to intall
 *  src_is_buffer           [IN]        memory-based handling; if it's true, src_path is a memory location
 *  src_is_virtual          [IN]        source file is a virtual file
 *  src_need_kept           [IN]        to keep source file
 *  dest_path               [IN/OUT]    path to put encoded result
 *  dest_buffer_len         [IN]        buffer size of dest_path, to avoid memory corruption
 *  dest_need_kept          [IN]        to output encoded result or not
 *  dest_content_type       [OUT]       content type of encoded result
 *  plaintext_len           [OUT]       length of plaintext in source file, only for DRM message
 *  plaintext_offset_in_dm  [OUT]       offset of plaintext in source file, only for DRM message
 *  callback                [IN]        callback after finish to install; must be given if is_async is true
 * RETURNS
 *  kal_int32
 *****************************************************************************/
kal_int32 DRM_install_object(kal_bool is_async, 
                             kal_uint8 *content_type, 
                             kal_uint8 *serial,
                             kal_wchar *src_path,
                             kal_bool src_is_buffer,
                             kal_bool src_is_virtual,
                             kal_bool src_need_kept,
                             kal_wchar* dest_path,
                             kal_uint32 dest_buffer_len,
                             kal_bool dest_need_kept,
                             kal_char *dest_content_type,
                             kal_uint32 *plaintext_len,
                             kal_uint32 *plaintext_offset_in_dm,
                             void (*callback) (kal_uint8 serial, kal_int32 result))
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;
    kal_int32 subtype;
    kal_int32 path_len = app_ucs2_strlen((kal_int8*)src_path);
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_trace(TRACE_GROUP_9, "install %s\n", (char*)content_type);

    if ((is_async && callback ==NULL) ||
        (content_type == NULL) ||
        (serial == NULL) ||
        (src_path == NULL) ||
        (dest_content_type == NULL) ||
        (src_path == dest_path) ||
        (dest_path == NULL && dest_need_kept) ||
        (dest_path && (dest_buffer_len == 0 || (dest_buffer_len < (path_len + 1) * 2 ))) ||
        (kal_wstrncmp(src_path, L"\\\\.\\", 4) == 0 && !src_is_virtual))
    {
        drm_trace(TRACE_GROUP_9, "DRM_RESULT_INVALID_PARA\n");
        return DRM_RESULT_INVALID_PARA;
    }

    *serial = drm_core_async_get_handle();
    subtype = mime_to_int((char*)content_type);
    if (subtype == MIME_SUBTYPE_DRM_MESSAGE)
    {
        kal_uint8 disk_path[10];
        FS_HANDLE input;
        kal_uint32 size = 0, disk_free_size = 0;
        FS_DiskInfo disk_info;
        
        /* parse input file */
        input = FS_Open(src_path, FS_READ_ONLY);
        if (input < FS_NO_ERROR)
        {
            return input;
        }
    
        memset(disk_path, 0, sizeof(disk_path));
        if (dest_path != NULL)
        {
            app_ucs2_strncpy((kal_int8*) disk_path, (kal_int8*)dest_path, 3);
        }
        else
        {
            app_ucs2_strncpy((kal_int8*) disk_path, (kal_int8*)src_path, 3);
        }
    
        FS_GetFileSize(input, &size);
#if 0        
        FS_GetDiskInfo((WCHAR*) disk_path, &disk_info, FS_DI_BASIC_INFO | FS_DI_FREE_SPACE);
        disk_free_size = disk_info.FreeClusters * disk_info.SectorsPerCluster * disk_info.BytesPerSector;
#else
		disk_free_size = FS_GetFreeSpace( (const WCHAR*)disk_path, FS_DI_BASIC_INFO | FS_DI_FREE_SPACE );
#endif            
        FS_Close(input);
        if (size > disk_free_size)
        {
            drm_trace(TRACE_GROUP_9, "DRM_RESULT_SIZE_TOO_BIG\n");
            return DRM_RESULT_SIZE_TOO_BIG;
        }
    }
    
    if (is_async)
    {
        drmt_install_object_req_struct *req = NULL;
        
        req = (drmt_install_object_req_struct*) construct_local_para(sizeof(drmt_install_object_req_struct), TD_RESET);

        req->mime_type = subtype;
        req->serial = *serial;
        req->src_path = src_path;
        req->src_is_buffer = src_is_buffer;
        req->src_is_virtual = src_is_virtual;
        req->src_need_kept = src_need_kept;
        req->dest_path = dest_path;
        req->dest_buffer_len = dest_buffer_len;
        req->dest_need_kept = dest_need_kept;
        req->dest_content_type = dest_content_type;
        req->plaintext_len = plaintext_len;
        req->plaintext_offset_in_dm = plaintext_offset_in_dm;
        req->callback = callback;
        drmt_send_msg(stack_get_active_module_id(), MOD_DRMT, MSG_ID_DRMT_INSTALL_OBJECT_REQ, (void*)req, NULL);
        result = DRM_PROC_ENCRYPT_BUSY;
        return result;
    }

    result = drm_core_install_object(subtype, 
                                     *serial, 
                                     src_path, 
                                     src_is_buffer, 
                                     src_is_virtual, 
                                     src_need_kept,
                                     dest_path,
                                     dest_buffer_len,
                                     dest_need_kept,
                                     dest_content_type,
                                     plaintext_len,
                                     plaintext_offset_in_dm);
    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_async_free_handle
 * DESCRIPTION
 *
 * PARAMETERS
 *  serial      [IN]
 * RETURNS
 *
 *****************************************************************************/
kal_bool DRM_async_free_handle(kal_uint8 serial)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_async_free_handle(serial);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_cancel_async_process
 * DESCRIPTION
 *
 * PARAMETERS
 *  serial      [IN]
 * RETURNS
 *
 *****************************************************************************/
kal_bool DRM_async_cancel_process(kal_uint8 serial)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drmt_abort_req_struct *msg;
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    msg = (drmt_abort_req_struct*) construct_local_para(sizeof(drmt_abort_req_struct), TD_RESET);

    drmt_send_msg(stack_get_active_module_id(), MOD_DRMT, MSG_ID_DRMT_ABORT_REQ, (void*)msg, NULL);

    return drm_core_async_cancel_process(serial);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_async_process_file
 * DESCRIPTION
 *  Process object asynchronously
 * PARAMETERS
 *  mime_type           []
 *  file_path           [?]
 *  new_path            [?]
 *  new_path_len        [IN]
 *  serial              [?]
 *  callback            [IN]
 * RETURNS
 *
 *****************************************************************************/
drm_proc_enum DRM_async_process_file(
            kal_uint8 *mime_type,
            kal_wchar *file_path,
            kal_wchar *new_path,
            kal_uint32 new_path_len,
            kal_uint8 *serial,
            void (*callback)(kal_uint8 serial, kal_int32 result, kal_uint32 offset, kal_uint32 len))
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return DRM_process_object(DRM_PROCESS_TYPE_ASYNC, mime_type, file_path, new_path, new_path_len, serial, callback);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_read_buffer
 * DESCRIPTION
 *  parse downloaded content to identify DRM content and populate rights information
 * PARAMETERS
 *  mime_type       [IN]
 *  buffer          [IN]
 *  size            [IN]
 *  info            [OUT]       MUST 4-bytes alignment
 *  mime_type(?)        [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_read_buffer(drm_rights_struct *rights, kal_uint8 *buffer, kal_uint32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_read_buffer(rights, buffer, size);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_process_buffer
 * DESCRIPTION
 *  parse downloaded content to identify DRM content and populate rights information
 * PARAMETERS
 *  mime_type       [IN]
 *  buffer          [IN]
 *  info            [OUT]       MUST 4-bytes alignment
 *  size(?)         [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_process_buffer(kal_uint8 *mime_type, kal_uint8 *buffer, drm_info_struct *info)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return DRM_process_object(DRM_PROCESS_TYPE_BUFFER, mime_type, buffer, info);
}

#ifdef __DRM_V02__
/*****************************************************************************
 * FUNCTION
 *  DRM_activate_file
 * DESCRIPTION
 * PARAMETERS
 *  file_path       [?]         [?]
 *  serial          [?]         [?]
 *  callback        [IN]        
 * RETURNS
 *****************************************************************************/
drm_proc_enum DRM_activate_file(kal_wchar *file_path, kal_uint8 *serial, drm_activate_callback callback)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drm_proc_enum result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_prompt_trace(MOD_MMI, "DRM_activate_file");
    result = drm_core_activate_file(file_path, serial, callback);
    return result;

}
#endif

/*****************************************************************************
 * FUNCTION
 *  DRM_process_file
 * DESCRIPTION
 *  parse downloaded content to identify DRM content and populate rights information
 * PARAMETERS
 *  mime_type       [IN]
 *  file_path       [IN]
 *  temp_use        [IN]
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_process_file(kal_uint8 *mime_type, kal_wchar *file_path, kal_bool temp_use)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drm_proc_enum result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    kal_prompt_trace(MOD_MMI, "DRM process file");
    result = DRM_process_virtual_file(mime_type, file_path, NULL);

    return result;

}


/*****************************************************************************
 * FUNCTION
 *  DRM_process_direct_file
 * DESCRIPTION
 *  parse downloaded content to identify DRM content and populate rights information
 * PARAMETERS
 *  mime_type       [IN]
 *  file_path       [IN]
 *  new_path        [IN]
 *  len             [IN]
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_process_direct_file(
                kal_uint8 *mime_type,
                kal_wchar *file_path,
                kal_wchar *new_path,
                kal_uint32 *len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return DRM_process_object(DRM_PROCESS_TYPE_VIRTUAL, mime_type, file_path, new_path, len);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_process_virtual_file
 * DESCRIPTION
 *  parse downloaded content to identify DRM content and populate rights information
 * PARAMETERS
 *  mime_type       [IN]
 *  file_path       [IN]
 *  new_path        [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
drm_proc_enum DRM_process_virtual_file(kal_uint8 *mime_type, kal_wchar *file_path, kal_wchar *new_path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return DRM_process_object(DRM_PROCESS_TYPE_NORMAL, mime_type, file_path, new_path);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_open_file
 * DESCRIPTION
 *  open file, if file is a DRM content, retreive rights information. otherwise simply open file.
 * PARAMETERS
 *  file_path       [IN]
 *  flags           [IN]
 *  permission      [IN]
 * RETURNS
 *  void
 *****************************************************************************/
FS_HANDLE DRM_open_file(kal_wchar *file_path, kal_uint32 flags, kal_uint8 permission)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    drmt_check_engine_ready();

    return drm_core_open_file(file_path, flags, permission);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_close_file
 * DESCRIPTION
 *  close file, if file is a DRM content, deinit rights information. otherwise simply close file.
 * PARAMETERS
 *  object      [IN]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_close_file(FS_HANDLE object)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_close_file(object);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_read_file
 * DESCRIPTION
 *  read data from file, if file is a DRM content, decrypt or seek to the right location before read
 * PARAMETERS
 *  source      [IN]
 *  buffer      [IN]
 *  size        [IN]
 *  length      [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_read_file(FS_HANDLE source, void *buffer, kal_uint32 size, kal_uint32 *length)
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_read_file(source, buffer, size, length);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_seek_file
 * DESCRIPTION
 *  seek file, if file is a DRM content, decrypt or seek to the right location for it
 * PARAMETERS
 *  source      [IN]
 *  offset      [IN]
 *  ref         [IN]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_seek_file(FS_HANDLE source, kal_int32 offset, kal_uint8 ref)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_seek_file(source, offset, ref);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_set_seek_hint
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 *  HintNum     [IN]        
 *  Hint        [?]         [?]
 *  source(?)       [IN](?)
 *  ref(?)          [IN](?)
 *  offset(?)       [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_set_seek_hint(FS_HANDLE handle, UINT HintNum, FS_FileLocationHint *Hint)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_set_seek_hint(handle, HintNum, Hint);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_file_size
 * DESCRIPTION
 *  get file size, if file is a DRM content, decrypt and remove header size
 * PARAMETERS
 *  source      [IN]
 *  size        [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_file_size(FS_HANDLE source, kal_uint32 *size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_file_size(source, size);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_file_pos
 * DESCRIPTION
 *  get file position, if file is a DRM content, decrypt and change file postition information by removing header
 * PARAMETERS
 *  source          [IN]
 *  pos             [OUT]
 * RETURNS
 *  zero for success            (1.0)
 *  negative for error code     (1.0/2.0)
 *  positive for content number (2.0)
 *****************************************************************************/
kal_int32 DRM_file_pos(FS_HANDLE source, kal_uint32 *pos)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_file_pos(source, pos);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_extension
 * DESCRIPTION
 *  get extension file name of a file path
 * PARAMETERS
 *  file_path       [IN]     
 * RETURNS
 *  void
 *****************************************************************************/
kal_wchar *DRM_get_extension(kal_wchar *file_path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 length, i;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    length = app_ucs2_strlen((const kal_int8*) file_path);

    for (i = length - 1; i >= 0; i--)
    {
        if (file_path[i] == '.')
        {
            return &file_path[i + 1];
        }
    }
    return NULL;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_consume_rights
 * DESCRIPTION
 *  consume DRM rights
 * PARAMETERS
 *  source          [IN]
 *  permission      [IN]
 *  callback        [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_consume_rights(FS_HANDLE source, kal_uint8 permission, void (*callback)(kal_int32 result, kal_int32 id))
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_consume_rights(source, permission, callback, KAL_FALSE);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_consume_timed_rights
 * DESCRIPTION
 * PARAMETERS
 *  source          [IN]        
 *  permission      [IN]        
 *  callback        [IN]        
 * RETURNS
 *****************************************************************************/
kal_int32 DRM_consume_timed_rights(
            FS_HANDLE source,
            kal_uint8 permission,
            void (*callback) (kal_int32 result, kal_int32 id))
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_consume_rights(source, permission, callback, KAL_TRUE);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_stop_consume
 * DESCRIPTION
 *  To stop rights consuming
 * PARAMETERS
 *  id      [IN]        
 *  input(?)        [IN]        File handle(?)
 *  sec_pass(?)     [IN]        How many seconds passed(?)
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_stop_consume(kal_int32 id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_stop_consume(id);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_pause_consume
 * DESCRIPTION
 *  To pause rights consuming
 * PARAMETERS
 *  id      [IN]        
 *  input(?)        [IN]        File handle(?)
 *  sec_pass(?)     [IN]        How many seconds passed(?)
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_pause_consume(kal_int32 id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_pause_consume(id);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_resume_consume
 * DESCRIPTION
 *  To resume rights consuming
 * PARAMETERS
 *  id      [IN]        
 *  input(?)        [IN]        File handle(?)
 *  sec_pass(?)     [IN]        How many seconds passed(?)
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_resume_consume(kal_int32 id)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_resume_consume(id);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_content_type
 * DESCRIPTION
 *  get content type of a DRM file
 * PARAMETERS
 *  handle      [IN]        
 *  path        [?]         [?]
 *  input(?)        [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint8 *DRM_get_content_type(FS_HANDLE handle, kal_wchar *path)
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    applib_mime_type_struct entry;
    kal_uint8 *result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        handle = drm_core_open_file(path, FS_READ_ONLY, DRM_PERMISSION_ALL);
        if (handle < FS_NO_ERROR)
        {
            return NULL;
        }
    }

    if (drm_core_get_content_type(handle, &entry) == DRM_RESULT_OK)
    {
        result = (kal_uint8*) entry.mime_string;
    }
    else
    {
        result = NULL;
    }

    if (path)
    {
        drm_core_close_file(handle);
    }

    return result;

}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_content_ext
 * DESCRIPTION
 *  get content extension name of a DRM file
 * PARAMETERS
 *  handle      [IN]        
 *  path        [?]         [?]
 *  input(?)        [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_char *DRM_get_content_ext(FS_HANDLE handle, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    applib_mime_type_struct entry;
    kal_char *result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        handle = drm_core_open_file(path, FS_READ_ONLY, DRM_PERMISSION_ALL);
        if (handle < FS_NO_ERROR)
        {
            return NULL;
        }
    }

    if (drm_core_get_content_type(handle, &entry) == DRM_RESULT_OK)
    {
        result = entry.file_ext;
    }
    else
    {
        result = NULL;
    }

    if (path)
    {
        drm_core_close_file(handle);
    }

    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_object_method
 * DESCRIPTION
 *  get DRM method of a DRM content
 * PARAMETERS
 *  input       [IN]        
 *  path        [?]         [?]
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint8 DRM_get_object_method(FS_HANDLE input, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 method = DRM_METHOD_NONE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        input = drm_core_open_file(path, FS_READ_ONLY, DRM_PERMISSION_ALL);
        if (input < FS_NO_ERROR)
        {
            return DRM_METHOD_NONE;
        }

        method = drm_core_get_object_method(input);
        drm_core_close_file(input);
        return method;
    }
    else
    {
        return drm_core_get_object_method(input);
    }
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_rights_issuer
 * DESCRIPTION
 *  
 * PARAMETERS
 *  input       [IN]        
 *  buffer      [?]         [?]
 *  size        [?]         [?]
 * RETURNS
 *  
 *****************************************************************************/
kal_int32 DRM_get_rights_issuer(FS_HANDLE input, kal_char *buffer, kal_uint32 *size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_rights_issuer(input, buffer, size);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_permission_by_path
 * DESCRIPTION
 *  get corresponding permission type for content of specific mime type
 * PARAMETERS
 *  mime_string     [IN]
 *  mime_type       [IN]
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint8 DRM_get_permission_by_path(kal_wchar *filepath)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    applib_mime_type_struct *entry;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    entry = mime_get_file_type((kal_wchar*) filepath);
    if (entry == NULL)
    {
        return DRM_PERMISSION_NONE;
    }
    switch (entry->mime_type)
    {
        case MIME_TYPE_IMAGE:
            return DRM_PERMISSION_DISPLAY;

        case MIME_TYPE_AUDIO:
        case MIME_TYPE_VIDEO:
            return DRM_PERMISSION_PLAY;

        case MIME_TYPE_APPLICATION:
            if (entry->mime_type == MIME_SUBTYPE_JAD || entry->mime_type == MIME_SUBTYPE_JAR)
            {
                return DRM_PERMISSION_EXECUTE;
            }
            else
            {
                break;
            }
        default:
            break;
    }
    return DRM_PERMISSION_NONE;

}



/*****************************************************************************
 * FUNCTION
 *  DRM_get_permission
 * DESCRIPTION
 *  get corresponding permission type for content of specific mime type
 * PARAMETERS
 *  mime_string     [IN]        
 *  mime_type       [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint8 DRM_get_permission(kal_uint8 *mime_string, kal_uint32 mime_type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    applib_mime_type_struct *entry;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    entry = mime_type_look_up((kal_char*) mime_string, NULL, 0, mime_type);
    if (entry == NULL)
    {
        return DRM_PERMISSION_NONE;
    }
    switch (entry->mime_type)
    {
        case MIME_TYPE_IMAGE:
            return DRM_PERMISSION_DISPLAY;

        case MIME_TYPE_AUDIO:
        case MIME_TYPE_VIDEO:
            return DRM_PERMISSION_PLAY;

        case MIME_TYPE_APPLICATION:
            if (entry->mime_subtype == MIME_SUBTYPE_JAD || entry->mime_subtype == MIME_SUBTYPE_JAR)
            {
                return DRM_PERMISSION_EXECUTE;
            }
            else
            {
                break;
            }
        default:
            break;
    }
    return DRM_PERMISSION_NONE;

}


/*****************************************************************************
 * FUNCTION
 *  DRM_validate_permission
 * DESCRIPTION
 *  check DRM rights with specific permission
 * PARAMETERS
 *  handle          [IN]        Is already opened, it will also carry the error code of FS_Open.
 *  path            [?]         [?]
 *  permission      [IN]        If permission == DRM_PERMISSION_PLAY, this function will check the permission by current mime_type
 *  file_path(?)        [IN]            If file_path != NULL, means we should open the file here.(?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_bool DRM_validate_permission(FS_HANDLE handle, kal_wchar *path, kal_uint8 permission)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_bool result = KAL_TRUE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        handle = drm_core_open_file(path, FS_READ_ONLY, permission);
        if (handle < FS_NO_ERROR)
        {
            return KAL_FALSE;
        }
    }

    result = drm_core_validate_permission(handle, permission);

    if (path)
    {
        drm_core_close_file(handle);
    }

    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_validate_timed_permission
 * DESCRIPTION
 *  check DRM rights with specific permission
 * PARAMETERS
 *  handle          [IN]        Is already opened, it will also carry the error code of FS_Open.
 *  permission      [IN]        If permission == DRM_PERMISSION_PLAY, this function will check the permission by current mime_type
 * RETURNS
 *  void
 *****************************************************************************/
kal_bool DRM_validate_timed_permission(FS_HANDLE handle, kal_uint8 permission)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_bool result = KAL_TRUE;
    drm_constraint_struct cons;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#ifdef __DRM_MTK__    
    result = drm_core_validate_timed_permission(handle, permission);
#endif
    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_interdev_movable
 * DESCRIPTION
 * PARAMETERS
 *  handle      [IN]        
 *  path        [?]         [?]
 * RETURNS
 *****************************************************************************/
kal_bool DRM_interdev_movable(FS_HANDLE handle, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_bool result = KAL_TRUE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        handle = drm_core_open_file(path, FS_READ_ONLY, DRM_PERMISSION_ALL);
        if (handle < FS_NO_ERROR)
        {
            return KAL_TRUE;
        }

        result = drm_core_interdev_movable(handle);
        drm_core_close_file(handle);
        return result;
    }
    else
    {
        return drm_core_interdev_movable(handle);
    }
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_constraint
 * DESCRIPTION
 * PARAMETERS
 *  handle          [IN]        
 *  path            [?]         [?]
 *  permission      [IN]        
 *  cons            [?]         [?]
 * RETURNS
 *****************************************************************************/
kal_int32 DRM_get_constraint(FS_HANDLE handle, kal_wchar *path, kal_uint8 permission, drm_constraint_struct *cons)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        handle = drm_core_open_file(path, FS_READ_ONLY, permission);
        if (handle < FS_NO_ERROR)
        {
            return handle;
        }
    }

    result = drm_core_get_constraint(handle, permission, cons);

    if (path)
    {
        drm_core_close_file(handle);
    }

    return result;
}
/*****************************************************************************
 * FUNCTION
 *  DRM_is_drm_file
 * DESCRIPTION
 * PARAMETERS
 *  handle          [IN]
 *  path            [?]         [?]
 *  permission      [IN]
 *  cons            [?]         [?]
 * RETURNS
 *****************************************************************************/
kal_int32 DRM_is_drm_file(FS_HANDLE handle, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    if (DRM_get_object_method(handle, path) != DRM_METHOD_NONE)
    {
        return KAL_TRUE;
    }
    return KAL_FALSE;
}

#ifdef __DRM_V02__


/*****************************************************************************
 * FUNCTION
 *  DRM_is_archive
 * DESCRIPTION
 * PARAMETERS
 *  handle      [IN]        
 *  path        [?]         [?]
 * RETURNS
 *****************************************************************************/
kal_bool DRM_is_archive(FS_HANDLE handle, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_bool result = KAL_FALSE;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (path)
    {
        kal_wchar *sepa = kal_wstrrchr(path, (kal_int32) '\\');

        if (sepa && kal_wstrncmp(sepa - 4, L".odf\\", 5) == 0)
            return KAL_FALSE;

        handle = drm_core_open_file(path, FS_READ_ONLY, DRM_PERMISSION_ALL);
        if (handle < FS_NO_ERROR)
        {
            return KAL_FALSE;
        }
    }

    result = drm_core_is_archive(handle);

    if (path)
    {
        drm_core_close_file(handle);
    }

    return result;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_find_cntx
 * DESCRIPTION
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void *DRM_get_find_cntx(void)
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_find_cntx();
}


/*****************************************************************************
 * FUNCTION
 *  DRM_find_firstN
 * DESCRIPTION
 *  
 * PARAMETERS
 *  find_cntx           [?]         [?]
 *  file_path           [?]         [?]
 *  PatternArray        [?]         [?]
 *  PatternNum          [IN]        
 *  FileInfo            [?]         [?]
 *  FileName            [?]         [?]
 *  MaxLen              [IN]        
 *  EntryIndex          [IN]        
 *  object(?)           [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
FS_HANDLE DRM_find_firstN(
            void *find_cntx,
            kal_wchar *file_path,
            FS_Pattern_Struct *PatternArray,
            kal_uint32 PatternNum,
            drm_file_info_struct *FileInfo,
            kal_wchar *FileName,
            kal_uint32 MaxLen,
            kal_int32 EntryIndex)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_find_firstN(find_cntx, file_path, PatternArray, PatternNum, FileInfo, FileName, MaxLen, EntryIndex);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_FindFirstN
 * DESCRIPTION
 *  
 * PARAMETERS
 *  find_cntx       [?]         [?]
 *  handle          [IN]        
 *  FileInfo        [?]         [?]
 *  FileName        [?]         [?]
 *  MaxLen          [IN]        
 *  distance        [IN]        
 *  object(?)       [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_find_nextN(
            void *find_cntx,
            FS_HANDLE handle,
            drm_file_info_struct *FileInfo,
            kal_wchar *FileName,
            kal_uint32 MaxLen,
            kal_int32 distance)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_find_nextN(find_cntx, handle, FileInfo, FileName, MaxLen, distance);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_find_close
 * DESCRIPTION
 *  close file, if file is a DRM content, deinit rights information. otherwise simply close file.
 * PARAMETERS
 *  find_cntx       [?]         
 *  handle          [IN]        
 *  object(?)       [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_find_close(void *find_cntx, FS_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_find_close(find_cntx, handle);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_ro_num
 * DESCRIPTION
 *  To get rights objects number
 * PARAMETERS
 *  void
 * RETURNS
 *  Number of RO; -1 means error
 *****************************************************************************/
kal_int32 DRM_get_ro_num(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_ro_num();
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_ro_list
 * DESCRIPTION
 *  To get rights objects list
 * PARAMETERS
 *  index       [IN]        Index of ro list
 *  alias       [OUT]       Alias string of the specific ro
 *  size        [IN]        
 * RETURNS
 *  is dcf or not
 *****************************************************************************/
kal_bool DRM_get_ro_list(kal_int32 index, kal_char *alias, kal_int32 size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_ro(index, alias, size);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_ro_status
 * DESCRIPTION
 * PARAMETERS
 *  index       [IN]        
 * RETURNS
 *****************************************************************************/
kal_bool DRM_get_ro_status(kal_int32 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_ro_status(index);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_ro_shareability
 * DESCRIPTION
 * PARAMETERS
 *  index       [IN]        
 * RETURNS
 *****************************************************************************/
kal_bool DRM_get_ro_shareability(kal_int32 index)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_is_ro_shareable(index);
}

#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* 0 */ 


/*****************************************************************************
 * FUNCTION
 *  DRM_free_ro_list
 * DESCRIPTION
 *  To free rights objects list
 * PARAMETERS
 *  data        [?]     
 * RETURNS
 *  is dcf or not
 *****************************************************************************/
kal_bool DRM_free_ro_list(void *data)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_free_ro_list();
    return KAL_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_toggle_tracking
 * DESCRIPTION
 *  1. To turn on or off transation tracking
 *  2. To get current status
 * PARAMETERS
 *  to_get      [IN]        
 * RETURNS
 *  1. to_get == FALSE -> turn on/off transation tracking
 *  2. to_get == TRUE  -> return current status
 *****************************************************************************/
kal_bool DRM_toggle_tracking(kal_bool to_get)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_toggle_tracking(to_get);
}



/*****************************************************************************
 * FUNCTION
 *  DRM_get_domain_num
 * DESCRIPTION
 *  To get total doamin that DRM Agent in
 * PARAMETERS
 *  void
 * RETURNS
 *  number
 *****************************************************************************/
kal_int32 DRM_get_domain_num(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_domain_num();
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_trust_state
 * DESCRIPTION
 *  To get total doamin that DRM Agent in
 * PARAMETERS
 *  void
 * RETURNS
 *  number
 *****************************************************************************/
kal_bool DRM_get_trust_state(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_trust_state();
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_secure_time
 * DESCRIPTION
 *  To get secure time
 * PARAMETERS
 *  time        [?]     
 * RETURNS
 *  number
 *****************************************************************************/
kal_bool DRM_get_secure_time(applib_time_struct *time)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_get_stime(time);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_import_certificate
 * DESCRIPTION
 * PARAMETERS
 *  path        [?]     [?]
 * RETURNS
 *****************************************************************************/
kal_int32 DRM_import_certificate(kal_wchar *path)
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return drm_core_import_cer(path);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_update_local_time
 * DESCRIPTION
 * PARAMETERS
 *  path        [?]     [?]
 * RETURNS
 *****************************************************************************/
void DRM_update_local_time(applib_time_struct *old_time, applib_time_struct* new_time, kal_bool reliable)
{

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_update_local_time(old_time, new_time, reliable);
}

#endif /* __DRM_V02__ */

/*****************************************************************************
 * FUNCTION
 *  DRM_get_version
 * DESCRIPTION
 *  To get DRM Agent Version
 * PARAMETERS
 *  void
 * RETURNS
 *  [bit 8~15] Major Version (0 to 255)
 *  [bit 0~7]  Minor Version (0 to 255)
 *****************************************************************************/
kal_int32 DRM_get_version(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
#if defined(__DRM_V02__)
    return 0x00020000;
#elif defined(__DRM_V01__)
    return 0x00010000;
#endif
}

/*****************************************************************************
 * FUNCTION
 *  DRM_set_delivery_time
 * DESCRIPTION
 *  To keep delta time in rights object.
 * PARAMETERS
 *  info            [IN]       MUST 4-bytes alignment
 *  delta           [IN]       rights arrive time
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_set_delivery_time(drm_info_struct *info, kal_uint32 delta)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
		drm_core_set_delivery_time(info, delta);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_delivery_time
 * DESCRIPTION
 *  To keep delta time in rights object.
 * PARAMETERS
 *  handle            [IN]       
  * RETURNS
 *  void
 *****************************************************************************/
kal_uint32 DRM_get_delivery_time(FS_HANDLE handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
		return drm_core_get_delivery_time(handle);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_limit_type
 * DESCRIPTION
 *  To keep delta time in rights object.
 * PARAMETERS
 *  handle      [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint32 DRM_get_limit_type(FS_HANDLE handle, kal_uint8 permission)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drm_constraint_struct cons;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_core_get_constraint(handle, permission, &cons);    
    
    return cons.type;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_app_common_hdlr
 * DESCRIPTION
 *  To
 * PARAMETERS
 *  ilm     [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
kal_bool DRM_app_common_hdlr(void *ilm)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    ilm_struct *ilm_ptr = (ilm_struct*) ilm;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    switch (ilm_ptr->msg_id)
    {
        case MSG_ID_DRMT_PROCESS_OBJECT_CNF:
            DRM_app_process_obj_hdlr((void*)ilm_ptr->local_para_ptr);
            return KAL_TRUE;
        case MSG_ID_DRMT_INSTALL_OBJECT_CNF:
            DRM_app_install_obj_hdlr((void*)ilm_ptr->local_para_ptr);
            return KAL_TRUE;  
    #ifdef __DRM_V02__
        case MSG_ID_DRMT_RO_EXPIRY_IND:
            DRM_app_ro_expiry_hdlr((void*)ilm_ptr->local_para_ptr);
            return KAL_TRUE;
        case MSG_ID_DRMT_ACTIVATE_CNF:
            DRM_app_activate_hdlr((void*)ilm_ptr->local_para_ptr);
            return KAL_TRUE;
    #endif
        default:
            return KAL_FALSE;
    }
}


/*****************************************************************************
 * FUNCTION
 *  DRM_app_process_obj_hdlr
 * DESCRIPTION
 *  To
 * PARAMETERS
 *  local_para_ptr      [?]     [?]
 *  time(?)             [IN](?)
 *  reliable(?)         [IN]        System (not user) modification or not.(?)
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_app_process_obj_hdlr(void *local_para_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drmt_process_object_cnf_struct *cnf = (drmt_process_object_cnf_struct*) local_para_ptr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_trace(TRACE_GROUP_9, "[DRM] app_process_obj_hdlr");

    if (cnf->callback)
    {
        cnf->callback(cnf->serial, cnf->result, cnf->offset, cnf->len);
    }
}
/*****************************************************************************
 * FUNCTION
 *  DRM_app_install_obj_hdlr
 * DESCRIPTION
 *  To
 * PARAMETERS
 *  local_para_ptr      [?]     [?]
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_app_install_obj_hdlr(void *local_para_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drmt_install_object_cnf_struct *cnf = (drmt_install_object_cnf_struct*) local_para_ptr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    drm_trace(TRACE_GROUP_9, "[DRM] app_install_obj_hdlr");

    if (cnf->callback)
    {
        cnf->callback(cnf->serial, cnf->result);
    }
}

#ifdef __DRM_V02__
/*****************************************************************************
 * FUNCTION
 *  DRM_app_ro_expiry_hdlr
 * DESCRIPTION
 *  To 
 * PARAMETERS
 *  local_para_ptr      [?]         
 *  time          [IN]       
 *  reliable      [IN]       system (not user) modification or not.
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_app_ro_expiry_hdlr(void *local_para_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drmt_ro_expiry_ind_struct *rsp = (drmt_ro_expiry_ind_struct*) local_para_ptr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/


    if ( rsp->callback && rsp->id >= 0)
    {
        if( drm_core_consume_is_expired(rsp->id))
        {
            drm_trace(TRACE_GROUP_9, "[DRM] ro_expiry_hdlr expired");
            drm_core_clean_consume(rsp->id);
        }
        else
        {
            drm_trace(TRACE_GROUP_9, "[DRM] ro_expiry_hdlr");
            rsp->callback(rsp->result, rsp->id);
        }
    }
    else
    {
        ASSERT(0);
    }
}


/*****************************************************************************
 * FUNCTION
 *  DRM_app_activate_hdlr
 * DESCRIPTION
 *  To
 * PARAMETERS
 *  local_para_ptr      [?]     [?]
 *  time(?)             [IN](?)
 *  reliable(?)         [IN]        System (not user) modification or not.(?)
 * RETURNS
 *  void
 *****************************************************************************/
void DRM_app_activate_hdlr(void *local_para_ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    drmt_activate_cnf_struct *cnf = (drmt_activate_cnf_struct*) local_para_ptr;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    drm_trace(TRACE_GROUP_9, "[DRM] app_activate_hdlr");

    if (cnf->callback)
    {
        cnf->callback(cnf->serial, cnf->result);
    }
}
#endif


#else  /* __DRM_SUPPORT__ */ 
#include "syscomp_config.h"

/*****************************************************************************
 * FUNCTION
 *  DRM_open_file
 * DESCRIPTION
 *  open file, if file is a DRM content, retreive rights information. otherwise simply open file.
 * PARAMETERS
 *  file_path       [IN]
 *  flags           [IN]
 *  permission      [IN]
 * RETURNS
 *  void
 *****************************************************************************/
FS_HANDLE DRM_open_file(kal_wchar *file_path, kal_uint32 flags, kal_uint8 permission)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return FS_Open(file_path, flags);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_close_file
 * DESCRIPTION
 *  close file, if file is a DRM content, deinit rights information. otherwise simply close file.
 * PARAMETERS
 *  object      [IN]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_close_file(FS_HANDLE object)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return FS_Close(object);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_read_file
 * DESCRIPTION
 *  read data from file, if file is a DRM content, decrypt or seek to the right location before read
 * PARAMETERS
 *  source      [IN]
 *  buffer      [IN]
 *  size        [IN]
 *  length      [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_read_file(FS_HANDLE source, void *buffer, kal_uint32 size, kal_uint32 *length)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return FS_Read(source, buffer, size, length);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_seek_file
 * DESCRIPTION
 *  seek file, if file is a DRM content, decrypt or seek to the right location for it
 * PARAMETERS
 *  source      [IN]
 *  offset      [IN]
 *  ref         [IN]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_seek_file(FS_HANDLE source, kal_int32 offset, kal_uint8 ref)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return FS_Seek(source, offset, ref);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_set_seek_hint
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 *  HintNum     [IN]        
 *  Hint        [?]         [?]
 *  source(?)       [IN](?)
 *  ref(?)          [IN](?)
 *  offset(?)       [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_set_seek_hint(FS_HANDLE handle, UINT HintNum, FS_FileLocationHint *Hint)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return FS_SetSeekHint(handle, HintNum, Hint);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_file_size
 * DESCRIPTION
 *  get file size, if file is a DRM content, decrypt and remove header size
 * PARAMETERS
 *  source      [IN]
 *  size        [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_file_size(FS_HANDLE source, kal_uint32 *size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    
    return FS_GetFileSize(source, size);
}


/*****************************************************************************
 * FUNCTION
 *  DRM_file_pos
 * DESCRIPTION
 *  get file position, if file is a DRM content, decrypt and change file postition information by removing header
 * PARAMETERS
 *  source          [IN]
 *  pos             [OUT]
 * RETURNS
 *  zero for success            (1.0)
 *  negative for error code     (1.0/2.0)
 *  positive for content number (2.0)
 *****************************************************************************/
kal_int32 DRM_file_pos(FS_HANDLE source, kal_uint32 *pos)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

    return FS_GetFilePosition(source, pos);
}


/*****************************************************************************
 * FUNCTION
 *  drmt_create
 * DESCRIPTION
 *  DRM task create function
 * PARAMETERS
 *  handle      [IN]
 * RETURNS
 *  success or fail
 *****************************************************************************/
kal_bool drmt_create(comptask_handler_struct **handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return KAL_FALSE;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_get_content_type
 * DESCRIPTION
 *  Dummy function
 * PARAMETERS
 *  handle      [IN]        
 *  path        [?]         [?]
 *  input(?)        [IN](?)
 * RETURNS
 *  void
 *****************************************************************************/
kal_uint8 *DRM_get_content_type(FS_HANDLE handle, kal_wchar *path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return NULL;
}


/*****************************************************************************
 * FUNCTION
 *  DRM_process_virtual_file
 * DESCRIPTION
 *  Dummy function
 * PARAMETERS
 *  mime_type       [IN]
 *  file_path       [IN]
 *  new_path        [OUT]
 * RETURNS
 *  void
 *****************************************************************************/
kal_int32 DRM_process_virtual_file(kal_uint8 *mime_type, kal_wchar *file_path, kal_wchar *new_path)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    return -1;
}
#endif /* __DRM_SUPPORT__ */ 

